package drazing;

import java.awt.geom.Area;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

/**
 * Clase para tratar circuitos
 * @author xcubo
 */
public class Pista
{
    private GeneralPath pista_ext, pista_int;
    private Area asfalto, cesped;
    private Fondo terrain = null;
    private Rectangle2D linea_meta;
    private Obstaculo[] obstacles = new Obstaculo[50];
    private int num_obs = 0;

    public Pista(int num)
    {
        pista_ext = new GeneralPath();
        pista_int = new GeneralPath();

        //switch (num)
        //{
        //    case 1:
                //Fondo
                terrain = new Fondo(5000, 5000, "grass");
  
                pista_ext.moveTo(1000.0f,500.0f);
                pista_ext.lineTo(1850.0f,500.0f);                                                   //Recta
                pista_ext.curveTo(2100.0f, 510.0f,  2200.0f, 525.0f,  2200.0f, 900.0f);     //Curva 1 Derecha
                pista_ext.curveTo(2225.0f, 1350.0f, 1800.0f, 975.0f,  1800.0f, 800.0f);     //Curva 2 Horquilla Derecha
                pista_ext.curveTo(1800.0f, 775.0f,  1750.0f, 750.0f,  1700.0f, 800.0f);     //Curva 3 Horquilla Izquierda
                pista_ext.curveTo(1500.0f, 950.0f,  1400.0f, 1200.0f, 1400.0f, 1400.0f);    //Enlazadas 4,5 y 6
                pista_ext.curveTo(1400.0f, 1500.0f, 1500.0f, 1600.0f, 1600.0f, 1600.0f);        // 5 Izquierda
                pista_ext.curveTo(1900.0f, 1650.0f, 1900.0f, 1850.0f, 1600.0f, 1900.0f);        // 6 Derecha
                pista_ext.curveTo(1300.0f, 1950.0f, 800.0f,  1750.0f, 800.0f,  1500.0f);            // Se abre
                pista_ext.lineTo(800.0f,1100.0f);
                pista_ext.curveTo(790.0f,  950.0f,  810.0f,  1000.0f,   925.0f,  950.0f);     //Chicane
                pista_ext.curveTo(1025.0f,  900.0f,  800.0f,  750.0f,    800.0f,  700.0f);
                pista_ext.curveTo(750.0f, 550.0f,   900.0f,  500.0f,    950.0f,  500.0f);        //Curva Meta 90º
                pista_ext.closePath();

                pista_int.moveTo(975.0f,600.0f);
                pista_int.lineTo(1850.0f,600.0f);                                                   //Recta
                pista_int.curveTo(2000.0f, 610.0f,  2100.0f, 625.0f,  2100.0f, 900.0f);     //Curva 1 Derecha
                pista_int.curveTo(2125.0f, 1150.0f, 1900.0f, 850.0f,  1900.0f, 800.0f);     //Curva 2 Horquilla Derecha
                pista_int.curveTo(1900.0f, 675.0f,  1700.0f, 675.0f,  1575.0f, 775.0f);     //Curva 3 Horquilla Izquierda
                pista_int.curveTo(1350.0f, 950.0f,  1300.0f, 1200.0f, 1300.0f, 1400.0f);    //Enlazadas 4,5 y 6
                pista_int.curveTo(1300.0f, 1500.0f, 1400.0f, 1700.0f, 1600.0f, 1700.0f);        // 5 Izquierda
                pista_int.curveTo(1750.0f, 1700.0f, 1750.0f, 1800.0f, 1600.0f, 1800.0f);        // 6 Derecha
                pista_int.curveTo(1300.0f, 1850.0f, 900.0f,  1650.0f, 900.0f,  1500.0f);            // Se abre
                pista_int.lineTo(900.0f,1050.0f);
                pista_int.curveTo(900.0f,  1050.0f, 910.0f,  1000.0f,   1000.0f,  1000.0f);     //Chicane
                pista_int.curveTo(1200.0f,  1000.0f, 925.0f,  750.0f,   900.0f,  700.0f);
                pista_int.curveTo(850.0f,  600.0f,  900.0f,  600.0f,    950.0f,  600.0f);        //Curva Meta 90º
                pista_int.closePath();

                obstacles[0] = new Obstaculo(new Rectangle2D.Double(1410, 480, 40, 20),0);    //Pilares puente
                obstacles[1] = new Obstaculo(new Rectangle2D.Double(690, 390,  1710, 20),1);    //Bridgestone
                obstacles[2] = new Obstaculo(new Rectangle2D.Double(2400, 390,  20, 1670),2);
                obstacles[3] = new Obstaculo(new Rectangle2D.Double(690, 2040,  1710, 20),1);
                obstacles[4] = new Obstaculo(new Rectangle2D.Double(690, 390,  20, 1650),3);
                obstacles[5] = new Obstaculo(new Rectangle2D.Double(900, 625, 1070, 20),4);     //F1
                obstacles[6] = new Obstaculo(new Rectangle2D.Double(2032, 825, 50, 150),5);     //Michelin
                obstacles[7] = new Obstaculo(new Rectangle2D.Double(735, 895, 200, 40),6);     //elf
                obstacles[8] = new Obstaculo(new Rectangle2D.Double(1200,650,15,1070),2);       //bridgestone trasversal
                obstacles[9] = new Obstaculo(new Rectangle2D.Double(1750,1200,550,20),8);       //barreras horquilla1
                obstacles[10] = new Obstaculo(new Rectangle2D.Double(1750,780,30,425),8);
                obstacles[11] = new Obstaculo(new Rectangle2D.Double(1250, 1650, 100, 50),7);    //Marlboro
                obstacles[12] = new Obstaculo(new Rectangle2D.Double(1410, 600, 40, 20),8);   // Segundo pilar
                obstacles[13] = new Obstaculo(new Rectangle2D.Double(2900, 625, 1070, 20),8);    //Ultima chicane
                obstacles[14] = new Obstaculo(new Rectangle2D.Double(1300, 1728, 400, 40),8);    //3ª horquilla
                num_obs = 15;

                linea_meta = new Rectangle2D.Float(1400, 500, 10.0f, 100.0f);

        //        break;
        //}
        
        asfalto = new Area(pista_ext);
        asfalto.subtract(new Area(pista_int));
        cesped = new Area(terrain.getLienzo());
        cesped.subtract(asfalto);
        

    }
    public Area getBackground()
    {
        return cesped;
    }
    public Area getTrack ()
    {
        return asfalto;
    }
    public boolean contains(float x, float y)
    {
       return asfalto.contains(new Point2D.Float(x, y));
    }

    /**
     * Devuelve el límite de pista indicado, 0 (interna) o 1 (externa)
     * @param a Pista interna (0) o externa (1)
     * @return Límite de pista
     */
    public GeneralPath getTrackBorders(int a)
    {
        assert(a == 0 || a == 1);
        GeneralPath gp = null;
        if (a == 0)
            gp = pista_int;
        else if (a == 1)
            gp = pista_ext;
        return gp;
    }

    /**
     * Devuelve la superficie indicada, 0 (cesped) o 1 (asfalto)
     * @param a Cesped (0) o asfalto (1)
     * @return Superficie
     */
    public Area getSurface(int a)
    {
        assert(a == 0 || a == 1);
        Area ar = null;
        if (a == 0)
            ar = cesped;
        else if (a == 1)
            ar = asfalto;
        return ar;
    }
    public Fondo getTerrain()
    {
        return terrain;
    }
    public Rectangle2D getEndLine()
    {
        return linea_meta;
    }
    public int getNumObs()
    {
        return num_obs;
    }
    public Obstaculo[] getObstacles()
    {
        return obstacles;
    }
    public Obstaculo getObstacles(int i)
    {
        return obstacles[i];
    }
}
